package com.tql.hualala.service;

import com.ruoyi.common.core.web.domain.CommonEntity;
import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.redis.service.RedisService;
import com.tql.hualala.HuaLaLaApi;
import com.tql.hualala.mapper.HllShopMapper;
import com.tql.hualala.mapper.HllShopMenuMapper;
import com.tql.hualala.bean.FoodBean;
import com.tql.hualala.bean.ShopBean;
import com.tql.hualala.bean.UnitsBean;
import com.tql.hualala.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

@Service

public class HualalaShopService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private HllShopMapper hllShopMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private HllShopMenuMapper hllShopMenuMapper;

    /**
     * 从哗啦啦云平台获取同庆楼集团下所有的门店，
     * 先清空 主库 店铺表所有数据 再插入
     * @return
     */
    @Transactional
    @Master
    public int saveAllShop() {
        List<ShopBean> list = HuaLaLaApi.getDocAllShops(CommonUtil.GROUP_ID);
        hllShopMapper.truncateHllShop();
        int count = hllShopMapper.insertHllShopBean(list);
        logger.info("HualalaShopService-saveAllShop-count-" + count);
        return count;
    }
    @Transactional
    @Master
    public void saveShopMenu(Long shopId) {
        List<FoodBean> list=HuaLaLaApi.getMenu(CommonUtil.GROUP_ID,shopId);
        logger.info("saveShopMenu-------shopId---{}",shopId);
        if(null==list)return;
        String[] foodIdArray=list.stream().map(FoodBean::getFoodID).toArray(String[]::new);
        //先清空
        hllShopMenuMapper.truncateHllShopMenu(CommonUtil.GROUP_ID,shopId);
        hllShopMenuMapper.deleteFoodUnit(foodIdArray);
        try {
            for (FoodBean food : list) {
                food.setOrigin(CommonUtil.Food_Origin_Type_HUALALA);
                List<UnitsBean> units = food.getUnits();
                int count = hllShopMenuMapper.insertHllShopFood(food);
                int count1 = hllShopMenuMapper.insertHllFoodUnits(units);
            }
        }catch (Exception ex)
        {
            logger.info("saveShopMenu--执行数据库插入异常信息{}----------{}",shopId,ex.getMessage());
        }

    }
    //查询那些门店已经开通了本系统的服务 ,kingdee对应的门店编码
    @Master
    public List<String> getEnabledShop() {
        List<String> enabledShops =null;
        try {
            enabledShops = redisService.getCacheList(CommonUtil.Enabled_ShopsForHll);
        }catch (Exception ex)
        {
            logger.info("HualalaShopService--getEnabledShop--"+ex.getMessage());
            ex.printStackTrace();
            List<String> shops = hllShopMapper.getEnabledShop();
            return shops;
        }
        if (CollectionUtils.isEmpty(enabledShops)) {
            List<String> shops = hllShopMapper.getEnabledShop();
            if (CollectionUtils.isEmpty(shops)) return null;
            //redis持久化
            redisService.setCacheList(CommonUtil.Enabled_ShopsForHll, shops);
            return shops;
        } else {
            return enabledShops;
        }

    }

@Transactional
@Master
    public void updateFood(CommonEntity entity) {
    List<FoodBean> list = HuaLaLaApi.getMenu(CommonUtil.GROUP_ID, entity.getShopId());
//        logger.info("updateFood-------shopId---{}",entity.getShopId());
//        if(null==list)return;
//        String[] foodIdArray=list.stream().map(FoodBean::getFoodID).toArray(String[]::new);

    String foodCode = entity.getFoodCode();
    Optional<FoodBean> result = list.stream()
            .filter(food -> food.getFoodCode().equals(foodCode))
            .findFirst();
    FoodBean foodBean =result.isPresent()?result.get():null;
    if (null != foodBean) {
        List<UnitsBean> units = foodBean.getUnits();
        hllShopMenuMapper.deleteFoodByFoodCode(foodBean);
        int count = hllShopMenuMapper.insertHllShopFood(foodBean);
        String[] foodIdArray = new String[]{foodBean.getFoodID()};
        hllShopMenuMapper.deleteFoodUnit(foodIdArray);
        int count1 = hllShopMenuMapper.insertHllFoodUnits(units);
    }


}


        //先清空
 //     hllShopMenuMapper.truncateHllShopMenu(CommonUtil.GROUP_ID,shopId);
//        hllShopMenuMapper.deleteFoodUnit(foodIdArray);
//        try {
//            for (FoodBean food : list) {
//                food.setOrigin(CommonUtil.Food_Origin_Type_HUALALA);
//                List<UnitsBean> units = food.getUnits();
//                int count = hllShopMenuMapper.insertHllShopFood(food);
//                int count1 = hllShopMenuMapper.insertHllFoodUnits(units);
//            }
//        }catch (Exception ex)
//        {
//            logger.info("saveShopMenu--执行数据库插入异常信息{}----------{}",shopId,ex.getMessage());
//        }
//    }
}
